<?php

class ecBase
{

  //connections DB
  public $master;
  public $slave;
  public $tableName;
  //array of errors
  public $arrayErrors = array();
  //array of elements tables
  public $arrayKeys = array();

  public function __construct($array = array())
  {
    //init db instance
    $this->master = ecConnectionDatabase::getDbMaster();
    $this->slave = ecConnectionDatabase::getDbSlave();

    //setVar in constructor
    $this->callSetVar($array);
  }

  public function callSetVar($array)
  {
    foreach ($this->arrayKeys as $key => $value)
    {
      if (array_key_exists($key, $array))
      {
        $methodName = 'set' . sfInflector::camelize($key);
        if (!empty($array[$key]))
          $this->$methodName($array[$key]);
      }
    }
  }

  public function init($array = array(), $classKeys = array())
  {
    $this->arrayKeys = array_merge($this->arrayKeys, $classKeys);

    foreach ($array as $key => $value)
    {
      foreach ($this->arrayKeys as $keys => $values)
      {
        if ($key == $keys)
        {
          if($values == 1)
          {
            if(empty($value))
              array_push($this->arrayErrors, 'keys '.$key.' is required');
          }
        }
      }
    }
  }

  public function getValuesArray()
  {
    $array = array();

    foreach ($this->arrayKeys as $key => $value)
    {
      $methodName = 'get' . sfInflector::camelize($key);

      if ($this->$methodName())
        $array[$key] = $this->$methodName();
    }

    return $array;
  }

  public function add($table, $array= array())
  {
    $quoteArray = array();
    if (sizeof($array) > 0)
    {
      foreach ($array as $key => $value)
      {
        $quoteArrayKey[] = $key;
        $quoteArrayValue[] = $this->master->quote(Secure::data($value));
      }

      if (array_key_exists('state', $this->arrayKeys))
      {
        $quoteArrayKey[] = 'state';
        $quoteArrayValue[] = $this->master->quote('0');

        $date = new DateTime();

        $quoteArrayKey[] = 'created_at';
        $quoteArrayValue[] = $this->master->quote($date->format('Y-m-d H:i:s'));
      }

      $this->master->insert($table, implode(' , ', $quoteArrayKey), implode(' , ', $quoteArrayValue));

      return $this->master->lastInsertId();
    }
    return false;
  }

  public function addReplace($table, $array= array())
  {
    $quoteArray = array();
    if (sizeof($array) > 0)
    {
      foreach ($array as $key => $value)
      {
        $quoteArrayKey[] = $key;
        $quoteArrayValue[] = $this->master->quote(Secure::data($value));
      }

      if (array_key_exists('state', $this->arrayKeys))
      {
        $quoteArrayKey[] = 'state';
        $quoteArrayValue[] = $this->master->quote('0');

        $date = new DateTime();

        $quoteArrayKey[] = 'created_at';
        $quoteArrayValue[] = $this->master->quote($date->format('Y-m-d H:i:s'));
      }

      $this->master->replace($table, implode(' , ', $quoteArrayKey), implode(' , ', $quoteArrayValue));

      return $this->master->lastInsertId();
    }
    return false;
  }

  public function update($table, $array=array(), $conditions = array())
  {
    if (sizeof($array) > 0)
    {
      foreach ($array as $key => $value)
      {
        $quoteArrayKeyValue[] = $key . ' = ' . $this->master->quote(Secure::data($value));
      }

      if (array_key_exists('state', $this->arrayKeys))
      {
        $quoteArrayKeyValue[] = 'state = 0';

        $date = new DateTime();

        $quoteArrayKeyValue[] = 'updated_at = ' . $this->master->quote($date->format('Y-m-d H:i:s'));
      }

      if (sizeof($conditions) > 0)
      {
        foreach ($conditions as $key => $value)
        {
          $quoteConditionKeyValue[] = $key . ' = ' . $this->master->quote(Secure::data($value));
        }
      }

      return $this->master->update($table, implode(' , ', $quoteArrayKeyValue), implode(' AND ', $quoteConditionKeyValue));
    }
    return false;
  }

  public function select($table, $array=array(), $conditions = array(), $object='slave', $tri)
  {
    $quoteConditionKeyValue = array();
    if (sizeof($array) > 0)
    {
      if (sizeof($conditions) > 0)
      {
        foreach ($conditions as $key => $value)
        {
          $quoteConditionKeyValue[] = $key . ' = ' . $this->$object->quote(Secure::data($value));
        }
      }

      if (array_key_exists('state', $this->arrayKeys))
      {
        $quoteConditionKeyValue[] = 'state = 0';
      }

      return $this->$object->select($table, implode(' , ', $array), implode(' AND ', $quoteConditionKeyValue), $tri);
    }
    return false;
  }

  public function delete($table, $conditions = array())
  {
    if (array_key_exists('deleted_at', $this->arrayKeys))
    {
      $date = new DateTime();

      $array = array('deleted_at' => $date->format('Y-m-d H:i:s'), 'state' => 1);

      if (sizeof($array) > 0)
      {
        foreach ($array as $key => $value)
        {
          $quoteArrayKeyValue[] = $key . ' = ' . $this->master->quote(Secure::data($value));
        }

        if (sizeof($conditions) > 0)
        {
          foreach ($conditions as $key => $value)
          {
            $quoteConditionKeyValue[] = $key . ' = ' . $this->master->quote(Secure::data($value));
          }
          return $this->master->update($table, implode(' , ', $quoteArrayKeyValue), implode(' AND ', $quoteConditionKeyValue));
        }
      }
    }
    else
    {

      if (sizeof($conditions) > 0)
      {
        foreach ($conditions as $key => $value)
        {
          $quoteConditionKeyValue[] = $key . ' = ' . $this->master->quote(Secure::data($value));
        }

        return $this->master->delete($table, implode(' AND ', $quoteConditionKeyValue));
      }
    }
    return false;
  }

  public function count($field, $object='slave')
  {
    $quoteConditionKeyValue = array();
    if (array_key_exists('state', $this->arrayKeys))
    {
      $quoteConditionKeyValue[] = 'state = 0';
    }
    $value = $this->$object->select($this->tableName, 'count(' . $field . ') as total', implode(' AND ', $quoteConditionKeyValue))->fetchAssoc();

    return $value['total'];
  }

}
